package com.smalls.newvideotwo.db;

import android.util.Log;

import com.smalls.newvideotwo.base.BaseActivity;
import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.DateUtil;

import org.xutils.DbManager;
import org.xutils.db.sqlite.WhereBuilder;
import org.xutils.ex.DbException;

import java.util.Date;
import java.util.List;

public class VodDao {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + "VodDao":"VodDao";
    private DbManager db = DbUtils.getDbManager(DbUtils.DB_NAME_USER_VOD);

    public void addAlbums(Album album) {
        try {
            this.db.saveOrUpdate(album);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void addAllMovies(List<Album>list) {
        try {
            this.db.saveOrUpdate(list);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public Album queryAlbumByMovieId(String category_id) {
        Album albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("movie_id", "in", new String[]{category_id + ""}).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByCategoryId(String category_id) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("category_id", "in", new String[]{category_id + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByCategoryIdAndPinyin(String str,String category_id) {
        List<Album> albumList = null;
        Log.i(TAG,"queryAlbumByCategoryIdAndPinyin str="+str+" category_id="+category_id);
        try {
            albumList = this.db.selector(Album.class).where("short_name","like",str+"%").findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        Log.i(TAG,"queryAlbumByCategoryIdAndPinyin albumList="+albumList);
        return albumList;
    }

    public List<Album> queryAlbumByFilter(String country, String tag, String year) {
        List<Album> albumList = null;
        WhereBuilder b = getFilter(country,tag,year);
        Log.i(TAG,"queryAlbumByFilter country="+country+" tag="+tag+" year="+year+" b="+b);
        try {
            albumList = this.db.selector(Album.class).where(getFilter(country,tag,year)).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        Log.i(TAG,"queryAlbumByFilter albumList="+albumList);
        return albumList;
    }

    private WhereBuilder getFilter(String country, String tag, String year) {
        if(country!=null) {
            if(tag!=null) {
                if(year!=null) {
                    return WhereBuilder.b("country", "like", "%"+country+"%").
                            and("genres", "like", "%"+tag+"%").
                            and("release_year", "=", Integer.valueOf(year));
                } else {
                    return WhereBuilder.b("country", "like", "%"+country+"%").
                            and("genres", "like", "%"+tag+"%");
                }
            } else {
                if(year!=null) {
                    return WhereBuilder.b("country", "like", "%"+country+"%").
                            and("release_year", "=", Integer.valueOf(year));
                } else {
                    return WhereBuilder.b("country", "like", "%"+country+"%");
                }
            }
        } else {
            if(tag!=null) {
                if(year!=null) {
                    return WhereBuilder.b("genres", "like", "%"+tag+"%").
                            and("release_year", "=", Integer.valueOf(year));
                } else {
                    return WhereBuilder.b("genres", "like", "%"+tag+"%");
                }
            } else {
                if(year!=null) {
                    return WhereBuilder.b("release_year", "=", Integer.valueOf(year));
                } else {
                    return null;
                }
            }
        }
    }

    public List<Album> queryAlbumAll() {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }
    public List<Album> queryAlbumAllByType(String mediaType) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("media_type","in",new String[]{mediaType + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByName(String titile,String mediaType) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("title", "like", "["+titile+"]").and("media_type","in",new String[]{mediaType + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByActor(String actor,String mediaType) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("actors", "like", "%"+actor+"%").and("media_type","in",new String[]{mediaType+ ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByDirector(String director,String mediaType) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("director", "like", "%"+director+"%").and("media_type","in",new String[]{mediaType+ ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Album> queryAlbumByGenres(String regres,String mediaType) {
        List<Album> albumList = null;
        try {
            albumList = this.db.selector(Album.class).where("genres", "like", "%"+regres+"%").and("media_type","in",new String[]{mediaType+ ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Record> queryAlbumByType(int typeId) {
        List<Record> albumList = null;
        try {
            albumList = this.db.selector(Record.class).where("typeId", "in", new String[]{typeId + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public void deleteAlbum(Album album) {
        try {
            this.db.delete((Object) album);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteAll() {
        try {
            this.db.delete(Album.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteByTypeAndId(int typeId, String contentId) {
        try {
            this.db.delete(Record.class, WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("movie_id", "=", contentId));
//            if (typeId == Constant.TYPE_JILU) {
//                deleteAllLinks();
//            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void addRecords(Record album) {
        try {
            this.db.saveOrUpdate(album);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public Record queryRecordByContentId(String contentId) {
        Record album = null;
        try {
            return (Record) this.db.selector(Record.class).where("contentId", "in", new String[]{contentId}).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
            return album;
        }
    }

    public Record queryRecordByTypeAndId(String contentId, int typeId) {
        Record album = null;
        try {
            return (Record) this.db.selector(Record.class).where("typeId", "in", new String[]{typeId + ""}).and("movie_id", "in", new String[]{contentId}).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
            return album;
        }
    }

    public List<Record> queryRecordByType(int typeId) {
        List<Record> albumList = null;
        try {
            albumList = this.db.selector(Record.class).where("typeId", "in", new String[]{typeId + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }
    public List<Record> queryAllAppsByType(int typeId, int timeType) {
        List<Record> albumList = null;
        try {
            WhereBuilder where = getTimeRangle(typeId, timeType, getTimeRangle(timeType));
            LogUtil.d("vodDao", "where = " + where);
            albumList = this.db.selector(Record.class).where(where).findAll();
            Log.i("msg", "\u6253\u5370\u67e5\u8be2\u5230\u7684\u6570\u636e\u96c6\u5408=" + albumList);
            return albumList;
        } catch (DbException e) {
            e.printStackTrace();
            return albumList;
        }
    }

    public List<Record> queryAllAppsByType(int typeId, String category_id) {
        List<Record> albumList = null;
        try {
            WhereBuilder where = WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("category_id", "=", category_id);
            LogUtil.d("vodDao", "queryAllAppsByType where = " + where);
            albumList = this.db.selector(Record.class).where(where).findAll();
            Log.i("msg", "\u6253\u5370\u67e5\u8be2\u5230\u7684\u6570\u636e\u96c6\u5408=" + albumList);
            return albumList;
        } catch (DbException e) {
            e.printStackTrace();
            return albumList;
        }
    }

    public Record queryAllAppsByTypeAndMovieId(int typeId, String category_id) {
        Record albumList = null;
        try {
            WhereBuilder where = WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("movie_id", "=", category_id);
            LogUtil.d("vodDao", "queryAllAppsByType where = " + where);
            albumList = this.db.selector(Record.class).where(where).findFirst();
            Log.i("msg", "\u6253\u5370\u67e5\u8be2\u5230\u7684\u6570\u636e\u96c6\u5408=" + albumList);
            return albumList;
        } catch (DbException e) {
            e.printStackTrace();
            return albumList;
        }
    }
    public void deleteAllByType(int typeId) {
        try {
            this.db.delete(Record.class, WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)));
            if (typeId == Constant.TYPE_JILU) {
                deleteAllLinks();
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public void deleteRecord(Record album) {
        try {
            this.db.delete((Object) album);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteAllRecord() {
        try {
            this.db.delete(Record.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    private String[] getTimeRangle(int Type) {
        String[] str = new String[2];
        switch (Type) {
            case 0 /*0*/:
                str[0] = DateUtil.ConverToString(new Date());
                str[1] = str[0];
                break;
            case 1 /*1*/:
                str[0] = DateUtil.ConverToString(DateUtil.getDateBefore(new Date(), 1));
                str[1] = DateUtil.ConverToString(new Date());
                break;
            case 2 /*2*/:
                str[0] = DateUtil.ConverToString(DateUtil.getDateBefore(new Date(), 7));
                str[1] = DateUtil.ConverToString(DateUtil.getDateBefore(new Date(), 2));
                break;
            case 3 /*3*/:
                str[0] = DateUtil.ConverToString(DateUtil.getDateBefore(new Date(), 8));
                str[1] = str[0];
                break;
        }
        return str;
    }

    private WhereBuilder getTimeRangle(int typeId, int Type, String[] str) {
        if (str == null || str.length <= 0) {
            return null;
        }
        switch (Type) {
            case 0 /*0*/:
                return WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("time", ">=", str[0]);
            case 1 /*1*/:
                return WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("time", ">=", str[0]).and("time", "<", str[1]);
            case 2 /*2*/:
                return WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("time", ">=", str[0]).and("time", "<=", str[1]);
            case 3 /*3*/:
                return WhereBuilder.b("typeId", "=", Integer.valueOf(typeId)).and("time", ">=", Integer.valueOf(0)).and("time", "<=", str[0]);
            default:
                return null;
        }
    }

    ////category

    public void deleteAllCategory() {
        try {
            this.db.delete(Category.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void addCategorys(Category album) {
        try {
            this.db.saveOrUpdate(album);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<Category> queryCategoryByCategoryId(String category_id) {
        List<Category> albumList = null;
        try {
            albumList = this.db.selector(Category.class).where("category_id", "in", new String[]{category_id + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Category> queryCategoryByParentId(String parent_id) {
        List<Category> albumList = null;
        try {
            albumList = this.db.selector(Category.class).where("parent_id", "in", new String[]{parent_id + ""}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public Category queryMainCategoryByName(String parent_id) {
        Category albumList = null;
        try {
            albumList = this.db.selector(Category.class).where("title", "in", new String[]{parent_id + ""}).and("depth", "=", 1).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public List<Category> queryAllSubCategory() {
        List<Category> albumList = null;
        try {
            albumList = this.db.selector(Category.class).where("depth", "=", 1).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return albumList;
    }

    public void addLinks(Links link) {
        try {
            this.db.saveOrUpdate(link);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public List<Links> queryLinkById(String contentId) {
        List<Links> links = null;
        try {
            links =this.db.selector(Links.class).where("contentId", "in", new String[]{contentId}).findAll();
        } catch (DbException e) {
            e.printStackTrace();
        }
        return links;
    }

    public void deleteAllLinks() {
        try {
            this.db.delete(Links.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    public void deleteLinksByAlbumContentId(String contentId) {
        try {
            this.db.delete(Links.class, WhereBuilder.b("albumId", "=", contentId));
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

}
